package com.xrs.xrseducation.order.service.impl;

import com.xrs.xrseducation.order.Utils.CodecUtil;
import com.xrs.xrseducation.order.entity.Order;
import com.xrs.xrseducation.order.mapper.OrderMapper;
import com.xrs.xrseducation.order.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xrs.xrseducation.ordercourse.entity.Ordercourse;
import com.xrs.xrseducation.ordercourse.mapper.OrdercourseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-09-18
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrdercourseMapper ordercourseMapper;

    @Override
    public void addOrder(Order order){
        int b[] = new int[3];
        Integer sid =  order.getStuId();
        String number = CodecUtil.getOrderCode(sid);
        order.setSerial(number);
        order.setState(0);
        DateTimeFormatter stf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        order.setChangeTime(stf.format(LocalDateTime.now()));
        Integer a =  orderMapper.insert(order);
        if(a==1){
           b[0] = 4;
           b[1] = 5;
           b[2] = 6;
            /*List list = new ArrayList();
            list.add(b);*/
            for (int i=0;i<=b.length-1;i++){
                    Ordercourse ordercourse = new Ordercourse();
                    ordercourse.setSerid(number);
                    //要拿到前端选中的课程id
                    ordercourse.setCouid(b[i]);
                    //这是循环前端传过来的课程根据课程id数量来循环查询，
                    //然后载一一添加
                   Integer z = ordercourseMapper.insert(ordercourse);
            }
        }
    }

    @Override
    public void delOrder(String serial){
        boolean b = orderMapper.delOrder(serial);
        if(b == true){
            ordercourseMapper.delCou(serial);
        }

    }
}
